﻿using PC_Touch.DataOP;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using System.Threading;
using HslCommunication;

namespace PC_Touch
{
    public partial class SettingsFS : Form
    {
        public SettingsFS()
        {
            InitializeComponent();
            x = this.Width;
            y = this.Height;
            setTag(this);
            this.Resize += new EventHandler(this.Form_Resize);
            setZH_EN();
        }
        private void setZH_EN()
        {
            if (LanguageHelper.zh_enFlag == "中文")
            {
            }
            else
            {
                uiTitlePanel1.Text = "Fan Lighting";
                uiTitlePanel2.Text = "Heat Pump";
                uiTitlePanel3.Text = "RFID Displacement Cylinder";
                uiTitlePanel4.Text = "Motorized Ball Valve";

                uiMarkLabel1.Text = "Lighting";
                uiMarkLabel3.Text = "Fan";
                动球阀.Text = "MotorizedBallValveForLeftLine";
                uiMarkLabel4.Text = "MotorizedBallValveForRightLine";
                uiMarkLabel2.Text = "FumeFilter";
                uiMarkLabel12.Text = "HeatPump1";
                uiMarkLabel11.Text = "HeatPump2";
                uiMarkLabel8.Text = "HeatPump3";
                uiMarkLabel10.Text = "HeatPump4";

                uiMarkLabel13.Text = "MainValve";
                uiMarkLabel9.Text = "SafetyDoorForTestStation";
                uiMarkLabel7.Text = "SafetyDoorForCodingStation";
                uiMarkLabel5.Text = "LeftRFIDShiftCylinder";
                uiMarkLabel6.Text = "RightRFIDShiftCylinder";
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            ReadPLCStatuesOnce();

            //监听事件初始化
            //PLCHelper.TriggerEventInit();
            //PLC轮询读取触发信号

        }
        public static bool[] ByteToBoolArray(byte b)
        {
            bool[] bits = new bool[8];

            for (int i = 0; i < 8; i++)
            {
                bits[i] = (b & (1 << (7 - i))) != 0;
            }

            return bits;
        }
        private void ReadPLCStatuesOnce()
        {
            if (PLCHelper.siemensConnect.IsSuccess)
            {
                // var by = PLCHelper.siemensTcpNet.ReadBool("Q104.4",12).Content;//读取气阀状态

                var q100_109 = PLCHelper.siemensTcpNet.Read("Q100",10).Content;
                var q103 = ByteToBoolArray(q100_109[3]);
                var q104 = ByteToBoolArray(q100_109[4]);
                var q108 = ByteToBoolArray(q100_109[8]);
                var q109 = ByteToBoolArray(q100_109[9]);


                照明.Active = q109[3];// PLCHelper.siemensTcpNet.ReadBool("Q109.3").Content; 
               
                烟雾过滤器.Active = q109[5];//  PLCHelper.siemensTcpNet.ReadBool("Q109.5").Content;
                右侧管路电动球阀.Active = q108[5];//  PLCHelper.siemensTcpNet.ReadBool("Q108.5").Content;
                风扇.Active = q109[4];//  PLCHelper.siemensTcpNet.ReadBool("Q109.4").Content;
                左侧管路电动球阀.Active = q108[6];// PLCHelper.siemensTcpNet.ReadBool("Q108.6").Content;
                隔膜泵1.Active = q104[0];//  PLCHelper.siemensTcpNet.ReadBool("Q104.0").Content;
                隔膜泵3.Active = q104[1];//PLCHelper.siemensTcpNet.ReadBool("Q104.1").Content;
                隔膜泵2.Active = q104[2];// PLCHelper.siemensTcpNet.ReadBool("Q104.2").Content;
                隔膜泵4.Active = q104[3];//PLCHelper.siemensTcpNet.ReadBool("Q104.3").Content;

                主气阀.Active = PLCHelper.siemensTcpNet.ReadBool("Q0.0").Content;
                测试工位安全门.Active = q103[5];// PLCHelper.siemensTcpNet.ReadBool("Q103.5").Content;
                打码工位安全门.Active = q103[7];// PLCHelper.siemensTcpNet.ReadBool("Q103.7").Content;
                左侧RFID变位气缸.Active = q109[1];// PLCHelper.siemensTcpNet.ReadBool("Q109.1").Content;
                右侧RFID变位气缸.Active = q108[7];//PLCHelper.siemensTcpNet.ReadBool("Q108.7").Content;
            }
            else 
            {
                MessageBox.Show("PLC not connect!");
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            TaskManagement taskManagement = new TaskManagement();
            taskManagement.Show();
        }

        #region 控件缩放
        private float x = 1;
        private float y = 1;
        private void setTag(Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size;
                if (con.Controls.Count > 0)
                {
                    setTag(con);
                }
            }
        }
        private void setControls(float newx, float newy, Control cons)
        {
            foreach (Control con in cons.Controls)
            {
                if (con.Tag != null)
                {
                    string[] mytag = con.Tag.ToString().Split(new char[] { ';' });
                    con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx);//宽度
                    con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy);//高度
                    con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx);//左边距
                    con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy);//顶边距
                    Single currentSize = Convert.ToSingle(mytag[4]) * newy;//字体大小
                    con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit);
                    if (con.Controls.Count > 0)
                    {
                        setControls(newx, newy, con);
                    }
                }
            }
        }
        private void Form_Resize(object sender, EventArgs e)
        {
            float newx = (this.Width) / x;
            float newy = (this.Height) / y;
            setControls(newx, newy, this);
        }
        #endregion
        private void uiSwitch12_ValueChanged(object sender, bool value)
        {
            if (左侧管路电动球阀.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.16.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.16.5", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.16.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.16.6", false);
            }
        }
        private void uiSwitch1_ValueChanged(object sender, bool value)
        {
            if (照明.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.21.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.21.3", false);
            }
            else 
            {
                PLCHelper.siemensTcpNet.Write("DB10.21.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.21.4", false);
            }
        }

        private void uiSwitch4_ValueChanged(object sender, bool value)
        {
            if (烟雾过滤器.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.21.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.21.7", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.21.8", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.21.8", false);
            }
        }

        private void qf3_ValueChanged(object sender, bool value)
        {
            if (右侧管路电动球阀.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.16.7", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.16.7", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.17.0", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.17.0", false);
            }
        }

        private void qf4_ValueChanged(object sender, bool value)
        {
            if (风扇.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.21.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.21.5", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.21.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.21.6", false);
            }
        }

        private void 隔膜泵1_ValueChanged(object sender, bool value)
        {
            if (隔膜泵1.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.17.5", true);  
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.17.6", true);
            }
        }

        private void 隔膜泵2_ValueChanged(object sender, bool value)
        {
            if (隔膜泵2.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.17.7", true);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.18.0", true);
            }
        }

        private void 隔膜泵3_ValueChanged(object sender, bool value)
        {
            if (隔膜泵3.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.18.1", true);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.18.2", true);
            }
        }

        private void 隔膜泵4_ValueChanged(object sender, bool value)
        {
            if (隔膜泵4.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.18.3", true);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.18.4", true);
            }
        }

        private void 主气阀_ValueChanged(object sender, bool value)
        {
            if (主气阀.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.15.1", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.15.1", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.15.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.15.2", false);
            }
        }

        private void 测试工位安全门_ValueChanged(object sender, bool value)
        {
            if (测试工位安全门.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.15.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.15.3", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.15.4", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.15.4", false);
            }
        }

        private void 打码工位安全门_ValueChanged(object sender, bool value)
        {
            if (打码工位安全门.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.15.5", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.15.5", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.15.6", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.15.6", false);
            }
        }

        private void 左侧RFID变位气缸_ValueChanged(object sender, bool value)
        {
            if (左侧RFID变位气缸.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.1.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.1.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.1.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.1.3", false);
            }
        }

        private void 右侧RFID变位气缸_ValueChanged(object sender, bool value)
        {
            if (右侧RFID变位气缸.Active)
            {
                PLCHelper.siemensTcpNet.Write("DB10.3.2", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.3.2", false);
            }
            else
            {
                PLCHelper.siemensTcpNet.Write("DB10.3.3", true);
                Thread.Sleep(100);
                PLCHelper.siemensTcpNet.Write("DB10.3.3", false);
            }
        }
    }
}
